package tpaagp.scheduleus;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class FirstTime extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		setTitle(R.id.title);

		// Now get the text on the name field. If it's empty, pop a dialog
		// informing that, otherwise finish the activity
		Button done = (Button) findViewById(R.id.buttonDone);
		done.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				EditText e = (EditText) findViewById(R.id.nameTextField);
				String name = e.getText().toString();
				if (name.compareTo("") == 0) {
					//The name is empty
					alertEmptyName();
				}
				else {
					//We save the name, workgroup and URL base in the preferences file
					SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
					SharedPreferences.Editor ed = prefs.edit();
					
					ed.putBoolean("firstTime", false);
					ed.putString("Name", name);
					ed.putString("workgroup", "workgroup");
					ed.putString("URL", "http://87.219.168.155:3380/");
					ed.putBoolean("notificationsEnabled", true);
					
					//And we can create also the default availability
					String mon = "000000000000000000000000";
					String tue = "000000000000000000000000";
					String wed = "000000000000000000000000";
					String thu = "000000000000000000000000";
					String fri = "000000000000000000000000";
					String sat = "000000000000000000000000";
					String sun = "000000000000000000000000";
					
					ed.putString("mon", mon);
					ed.putString("tue", tue);
					ed.putString("wed", wed);
					ed.putString("thu", thu);
					ed.putString("fri", fri);
					ed.putString("sat", sat);
					ed.putString("sun", sun);
					
					if (!ed.commit()) {
						System.out.println("ERROR");
					}
					
					String user = prefs.getString("Name", "user");
					String urlS = prefs.getString("URL", "url");					
					String queryString = "disponibilidad.php?user="+user;
					HttpClient client = new DefaultHttpClient();
			        HttpGet request = new HttpGet(urlS+queryString);
					try {
						client.execute(request);
					} catch (ClientProtocolException es) {
						// TODO Auto-generated catch block
						es.printStackTrace();
					} catch (IOException es) {
						// TODO Auto-generated catch block
						es.printStackTrace();
					}			
					
					finish();
				}
			}
		});
	}
	
	public void alertEmptyName() {
		AlertDialog.Builder a = new AlertDialog.Builder(this);
		a.setMessage(R.string.emptyNameMessage).setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		a.show();
	}

}
